<?php
use yii\helpers\Url;
use yii\Web\View;
use app\assets\NewUserAsset;
use app\assets\StrategyResourceAsset;
NewUserAsset::register($this);
$this->title = $this->context->pageTitle.Yii::$app->params['titleSuffix'];
$this->params['breadcrumbs'] = [
  [
    'label' => \Yii::t('common', 'User'),
    'url' => ['user-list']
  ],
  \Yii::t('user', 'Edit User')
];
$jsMsgs = $this->context->loadJsMsgs([
  'users',
]);
$this->registerJs("VAM.setVar('LANGUAGE_JS', '".$jsMsgs."');", View::POS_HEAD);
?>
<div class="page-header">
  <h1>
    <?=\Yii::t('common', 'User')?>
    <small><?=\Yii::t('user', 'Edit User')?></small>
  </h1>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <i class="fa fa-edit"></i>
    <?=\Yii::t('user', 'Edit User')?>
    <ul class="list-unstyled pull-right">
      <li>
        <a href="#">
          <i class="fa fa-chevron-up"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="panel-body">
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <form class="form-horizontal" id="data-frm" method="POST" action="<?php echo Url::to(['users/user-edit-handle']);?>">
          <input class="hidden" type="text" name="id" value="<?= $user['id'] ?>">
          <fieldset>
            <legend><?=\Yii::t('user', 'Basic Info')?></legend>
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="user_name" >
                <em>*</em>
                <?=\Yii::t('user', 'Email')?>：
              </label>
              <div class="col-sm-10">
                <input class="form-control" id="user_email" type="text" name="email" value="<?= $user['email'] ?>" placeholder="<?=\Yii::t('user', 'Email')?>" readonly>
                <div class="help-block text-error" id="user_email_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="user_name" >
                <em>*</em>
                <?=\Yii::t('user', 'Name')?>：
              </label>
              <div class="col-sm-10">
                <input class="form-control" id="user_name" type="text" name="name" value="<?= $user['name'] ?>" placeholder="<?=\Yii::t('user', 'Name')?>">
                <div class="help-block text-error" id="user_name_err"></div>
              </div>
            </div>
            <!-- <div class="form-group">
              <label class="col-sm-2 control-label" for="user_pwd">
                <em>*</em>
                新密码：
              </label>
              <div class="col-sm-10">
                <input class="form-control" id="user_pwd" type="password" name="password" value="" placeholder="">
                <div class="help-block text-error" id="user_pwd_err"></div>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="confirm_pwd">
                <em>*</em>
                确认密码：
              </label>
              <div class="col-sm-10">
                <input class="form-control" id="confirm_pwd" type="password" name="confirm_password" value="" placeholder="">
                <div class="help-block text-error" id="confirm_pwd_err"></div>
              </div>
            </div> -->
            <div class="form-group">
              <label class="col-sm-2 control-label" for="user_type">
                <em>*</em>
                <?=\Yii::t('user', 'Type')?>：
              </label>
              <div class="col-sm-10">
                <label class="radio-inline">
                  <input type="radio" name="user_type" value="2" <?php if ($user['user_type'] == 2) echo "checked"; ?> > <?=\Yii::t('user', 'Operator')?>
                </label>
				<label class="radio-inline">
                  <input type="radio" name="user_type" value="3" <?php if ($user['user_type'] == 3) echo "checked"; ?> > <?=\Yii::t('user', 'Agent')?>
                </label>
                <label class="radio-inline">
                  <input type="radio" name="user_type" value="4" <?php if ($user['user_type'] == 4) echo "checked"; ?> > <?=\Yii::t('user', 'Advertiser')?>
                </label>
                <div class="help-block"></div>
              </div>
            </div>
			<!--
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="user_role">
                角色：
              </label>
              <div class="col-sm-10">
                <select class="form-control" id="user_role" name="is_root">
                  <option value="0" <?php if ($user['is_root'] == 0) echo "selected"; ?> >广告执行</option>
                  <option value="2" <?php if ($user['is_root'] == 2) echo "selected"; ?> >只读用户</option>
                  <option value="1" <?php if ($user['is_root'] == 1) echo "selected"; ?> >管理员</option>
                </select>
              </div>
            </div>
			-->
          </fieldset>
          <fieldset>
            <legend><?=\Yii::t('user', 'Resource')?></legend>
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="advertiser_resource">
                <?=\Yii::t('user', 'Resource')?>：
              </label>
              <div class="col-sm-10">
				<select class="form-control" id="advertiser_resource" name="advertiser_resource[]" placeholder="<?=\Yii::t('user', 'Please input keywords')?>..." multiple>
					<option value=""></option>
					<?php 
						$out = '';
						$isSelected = '';
						foreach($advertisers as $id => $advertiser) {
							$id = $advertiser['id'];
							$title = $advertiser['name'];						
							$isSelected = '';
							if (in_array($id, $domainAdvertiser)) {
								$isSelected = 'selected';
							}
							$out .= "<option value='$id' $isSelected>$title</option>";
						}
						echo $out;
					?>
				</select>
                <!-- <input class="form-control" id="advertiser_resource" type="text" name="advertiser_resource" value="" placeholder="请输入关键字..."> -->
                <div class="help-block text-error" id="advertiser_resource_err"></div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend><?=\Yii::t('user', 'Govern User')?></legend>
            <div class="form-group">
              <label class="col-sm-2 control-label pl0" for="govern_users">
                <?=\Yii::t('user', 'Govern User')?>:
              </label>
              <div class="col-sm-10">			  
				<select class="form-control" id="govern_users" name="govern_users[]" placeholder="<?=\Yii::t('user', 'Please input keywords')?>..." multiple>
					<option value=""></option>
					<?php 
						$out = '';
						$isSelected = '';
						foreach($subUsers as $id => $user) {
							$id = $user['id'];
							$title = $user['name'];						
							$isSelected = '';
							if (in_array($id, $domainUser)) {
								$isSelected = 'selected';
							}
							$out .= "<option value='$id' $isSelected>$title</option>";
						}
						echo $out;
					?>
				</select>
                <!-- <input class="form-control" id="govern_users" type="text" name="govern_users" value="" placeholder="请输入关键字..."> -->
                <div class="help-block text-error" id="govern_users_err"></div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend><?=\Yii::t('user', 'User Resource')?></legend>
            <div class="col-sm-6">
              <div class="form-group">
                <label for="filter-channel" class="col-sm-4 control-label pr0"><?=\Yii::t('user', 'Choose Channel')?>：</label>
                <div class="col-sm-8">
                  <select class="form-control" id="filter-channel" data-number="0" name="adnetwork_slot[]" multiple>
                    <?php
                      $adnetwork_slot = explode(',', $aUser['adnetwork_slot']);
                      $all = \Yii::t('user', 'All');
                      $selected = '';
                      if (in_array(0, $adnetwork_slot)) {
                        $selected = 'selected';
                      }
                      echo "<option value='0' $selected>$all</option>";
                      foreach ($adChannels as $adChannel) {
                        $id = $adChannel['id'];
                        $name = $adChannel['channel'];
                        $selected = '';
                        if (in_array($id, $adnetwork_slot)) {
                          $selected = 'selected';
                        }
                        echo "<option value='$id' $selected>$name</option>";
                      }

                    ?> 
                  </select>
                </div>
              </div>
            </div>
            <div class="col-sm-6">
              <div class="form-group">
                <label for="filter-traffic" class="col-sm-4 control-label pr0"><?=\Yii::t('user', 'Choose Traffic Type')?>：</label>
                <div class="col-sm-8">
                  <select class="form-control" id="filter-traffic" data-number="1" name="traffic_slot[]" multiple>
					<?php
						$traffic_slot = explode(',', $aUser['traffic_slot']);
						foreach($trafficTypes as $id => $type) {
							$selected = '';
							if (in_array($id, $traffic_slot)) {
								$selected = 'selected';
							}
							echo "<option value='$id' $selected>$type</option>";
						}
					?>
                  </select>
                </div>
              </div>
            </div>
            <div class="col-sm-12">
              <div class="form-group">
                <label for="filter_media" class="col-sm-2 control-label"><?=\Yii::t('user', 'Choose Media')?>：</label>
                <div class="col-sm-10">
                  <select class="form-control" id="filter_media" data-number="2" name="media_slot[]" multiple>
					<?php
						$media_slot = explode(',', $aUser['media_slot']);
						$selected = '';
						if (in_array(0, $media_slot)) $selected = 'selected';
						$all = \Yii::t('user', 'All');
						echo "<option value='0' $selected>$all</option>";
						foreach($medias as $media) {
							$id = $media['id'];
							$name = $media['media_name'];
							$selected = '';
							if (in_array($id, $media_slot)) $selected = 'selected';
							echo "<option value='$id' $selected>$name</option>";
						}
					?>
                  </select>
                </div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <div class="btn-form-group pull-right">
                  <button type="submit" class="btn btn-primary" data-loading-text="<i class='fa fa-spinner fa-spin'></i> <?=\Yii::t('common', 'Saving')?>..."><?=\Yii::t('common', 'Save')?></button>
                  <a href="<?php echo Url::to(['users/user-list']); ?>" class="btn btn-default"><?=\Yii::t('common', 'Back')?></a>
                </div>
              </div>
            </div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<?php
  $channelToTraffic = '{"万流客": {"id": "ch01", "value": [{"id": "trf01", "value": "PC-Display"}, {"id": "trf02", "value": "PC-Video"}]}, "VivaKi": {"id": "ch02", "value": [{"id": "trf03", "value": "Mobile-Video"}]}}';
  $trafficToMedia = Yii::getAlias('@web').'/response/users/mediaList.php';
  $this->registerJs("VAM.setVar('TRAFFIC_TO_MEDIA','$trafficToMedia');", View::POS_HEAD);
  $this->registerJs("VAM.setVar('CHANNEL_TO_TRAFFIC','$channelToTraffic');", View::POS_HEAD);
  
?>
<script type="text/javascript" >
  requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/site.js"], function(common) {
    requirejs(["<?= Yii::getAlias('@web/').Yii::$app->params['envDir'];?>/js/users/usersEdit.js"]);
  });
</script>
